package com.lcx.movie.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.Comment;
import com.lcx.movie.service.CommentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

//徐大大
@CrossOrigin(origins = "*")
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * 根据电影id查询出电影评论,展示评论对应的用户头像
     * 分页查询
     * @param movieId 电影id
     * @return 评论集合
     */
    @GetMapping("/selectCommentsByMovieId")
    public Rep selectCommentsByMovieId(@Param("page") int page,@Param("pageSize") int pageSize,
            @Param("movieId") Integer movieId) {
        // 开始分页
        PageHelper.startPage(page, pageSize);
        // 执行查询
        List<Comment> commentList = commentService.selectCommentsByMovieId(movieId);
        // 创建分页对象
        PageInfo<Comment> commentPageInfo = new PageInfo<>(commentList);
        // 返回对象
        return new Rep().addData("commentPageInfo", commentPageInfo);
    }

    /**
     * 新增评论
     * @param comment 评论实体
     * @return 受影响的行数
     */
    @PostMapping("/insertComment")
    public String insertComment(@RequestBody Comment comment) {
        int insertComment = commentService.insertComment(comment);
        if (insertComment > 0) {
            return "新增评论成功";
        } else {
            return "新增评论失败";
        }
    }

    /**
     * 删除评论[前端]
     * @param commentId 评论id
     * @return 受影响的行数
     */
    @GetMapping("/deleteComment")
    public String deleteComment(@Param("commentId") Integer commentId) {
        int deleteComment = commentService.deleteComment(commentId);
        if (deleteComment > 0) {
            return "删除评论成功";
        } else {
            return "删除评论失败";
        }
    }

    /**
     * 分页模糊查询评论表
     * @param page 当前页数
     * @param userName 用户名
     * @return rep对象
     */
    @GetMapping("/selectComment/{page}")
    public Rep selectCommentPage(@PathVariable("page")int page, @RequestParam(required = false)String userName){
        PageHelper.startPage(page,10);
        //执行查询
        List<Comment> list=this.commentService.selectCommentPage(userName);
        PageInfo<Comment>commentPageInfo=new PageInfo<>(list);
        return new Rep().addData("commentPageInfo",commentPageInfo);
    }
    /**
     *详细查询评论表
     * @param commentId 评论id
     * @return rep对象
     */
    @GetMapping("/selectCommentId")
    public Rep selectCommentId(int commentId){
        Comment comment=this.commentService.selectCommentId(commentId);
        return new Rep().addData("comment",comment);
    }
    /**
     * 修改评论表
     * @param comment 评论实体
     * @return 字符串信息
     */
    @PostMapping("/updateComment")
    public String updateComment(@RequestBody Comment comment){
        return this.commentService.updateComment(comment);
    }
    /**
     *  删除评论表
     * @param commentId 评论id
     * @return 字符串提示信息
     */
    @PostMapping("/deleteCommentId")
    public String deleteCommentId(@RequestParam("commentId")int commentId){
        return this.commentService.deleteCommentId(commentId);
    }


}
